package com.trepadora.flex
{
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetStream;

	public class CameraPanel extends VideoPanel
	{
		private var ns:NetStream;
		private var camera:Camera;
		private var microphone:Microphone;

		public function init(id:String, micName:String, cameraName:String, settings:Object):void
		{
			trace(cameraName + " " + micName)
			_streamID=id;

			ns=new NetStream(AVManager.getNc())

			var publish:Boolean=false;

			camera=AVManager.getCamera(cameraName);
			trace(camera)
			if (camera != null)
			{
				camera.setQuality(settings["bitrate"], 0);
				camera.setMode(settings["width"], settings["height"], settings["fps"], false);
				camera.setLoopback(true);

				ns.attachCamera(camera);
				video.attachCamera(camera);
				publish=true;
			}

			microphone=AVManager.getMicrophone(micName);

			if (microphone != null)
			{
				microphone.codec=settings["codec"];
				microphone.encodeQuality=settings["quality"];
				microphone.setUseEchoSuppression(true);
				ns.attachAudio(microphone);
				publish=true;
			}
			
			trace("PUBLISH VAR " + publish)
			
			if (publish)
			{
				trace("PUBLISH START")
				ns.publish(streamID);
			}
		}

		public function getCamera():Camera
		{
			return camera;
		}

		public function getMicrophone():Microphone
		{
			return microphone;
		}

		public function removeCamera():void
		{
			ns.attachCamera(null);
			video.attachCamera(null);
			videoDisplay.video=null;
			video=new Video(170, 135);
			videoDisplay.video=video;
			camera=null;
		}

		public function stop():void
		{
			ns.attachAudio(null);
			ns.attachCamera(null);
			video.attachCamera(null);
			ns.close();
			video.clear();
			camera=null;
			microphone=null;
			video=null;
		}

		public function equals(micName:String, cameraName:String, settings:Object):Boolean
		{
			var audioCorrect:Boolean=false;
			var videoCorrect:Boolean=false;

			if (camera == null && cameraName == null)
			{
				videoCorrect=true;
			}
			else if (camera != null && settings != null)
			{
				if (cameraName == camera.name)
				{
					var bandwidtch:int=settings["bitrate"];
					var width:int=settings["width"];
					var height:int=settings["height"];
					var fps:int=settings["fps"];
					//Alert.show((quality == camera.quality) + " " + (width == camera.width) + " " + (height == camera.height) + " " + (fps == camera.fps));
					if (bandwidtch == camera.bandwidth && width == camera.width && height == camera.height && fps == camera.fps)
					{
						//Alert.show("wideo się zgadza");
						videoCorrect=true;
					}
				}
			}

			if (microphone == null && micName == null)
			{
				videoCorrect=true;
			}
			else if (microphone != null && settings != null)
			{
				if (micName == microphone.name)
				{
					var codec:String=settings["codec"];
					var audioQuality:int=settings["quality"];

					if (codec == microphone.codec && microphone.encodeQuality == audioQuality)
					{
						//Alert.show("audio się zgadza");
						audioCorrect=true;
					}
				}
			}

			return audioCorrect && videoCorrect;
		}
	}
}